<template>
  <div>
    <el-container class="home-container">
      <el-header>
        <div style="display:flex; justify-content:space-between;width:14%">
          <div class="top">
            <img src="../assets/协同办公-img.png">
            <span slot="title">协同办公平台</span>
          </div>
          <div style="padding-top:4vh;width:5%">
            <el-link icon="el-icon-s-fold" @click="showaside = false" v-if="showaside"></el-link>
            <el-link icon="el-icon-s-unfold" @click="showaside = true" v-if="!showaside"></el-link>
          </div>
        </div>
        <!-- 顶部搜索框 -->
        <div class="search">
            <!-- style="margin-top: 15px;" -->
            <el-input placeholder="请输入内容" v-model="inputString" class="input-with-select">
              <el-button slot="append" icon="el-icon-search" @click="globalSearch"></el-button>
            </el-input>
          </div>
        <div>
          <!-- 快速新建 -->
          <!-- <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              <i class="el-icon-circle-plus-outline"></i>
              快速新建
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="a" icon="icon-office-flag">新建待办</el-dropdown-item>
              <el-dropdown-item command="b" icon="el-icon-date">新建日程</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown> -->
          <!-- 头像区域 -->
          <el-avatar :src="user.header" style="display:inline-block;position:relative;top:5px"></el-avatar>
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link" :model="user">
              {{user.name}}
              <i class="el-icon-arrow-down"></i>
            </span>
            <el-dropdown-menu
            slot="dropdown"
            v-if="this.$router.currentRoute.path != '/home'">
              <el-dropdown-item command="about" icon=" icon-office-creditcard">关于我的</el-dropdown-item>
              <el-dropdown-item command="loginout" icon="icon-office-switch">退出系统</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>
      <!-- 内容区域 -->
      <el-container>
        <!-- 侧边导航栏 start -->
        <aside-bar v-show="showaside"></aside-bar>
        <!-- 侧边导航栏 end -->
        <el-main>
          <!-- 右侧中心面包屑导航 -->
          <el-breadcrumb separator-class="el-icon-arrow-right" class="top-navigation">
            <!-- <el-breadcrumb-item v-for="item in levelList" :key="item.path">
            </el-breadcrumb-item> -->
            <el-breadcrumb-item v-for="item in levelList" :key="item.path" :to="item.path">
              {{item.name}}
            </el-breadcrumb-item>
          </el-breadcrumb>
          <!-- 路由占位符 -->
           <!-- 右侧中心内容区域 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
    <!-- 嵌套表格的对话框 -->
    <el-dialog :visible.sync="dialogTableVisible">
      <el-table
      :data="gridData"
      ref="gridData">
        <el-table-column
          type="index"
          :index="getIndex"
          fixed
          width="80"
          label="序号"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="id" label="编号" width="150"></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="infoType" label="类型" width="200"></el-table-column>
        <el-table-column prop="description" v-html="gridData.description" label="描述"></el-table-column>
      </el-table>
      <!-- 分页器 -->
      <div class="block" style="margin-top:15px; margin-right: 20px">
        <el-pagination
          align="right"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ASE from '../utils/utils'
export default {
  name: 'Home',
  data() {
    return {
      levelList: [], // 面包屑导航数据
      user: {},
      // 左侧菜单数据
      menulist: [],
      activeIndex: this.$route.path,
      isCollapse: true,
      showaside: true,
      inputString: '',
      dialogTableVisible: false,
      gridData: [{
        id: '',
        name: '',
        infoType: '',
        description: ''
      }],
      // *******翻页*********************
      NowApplyDataList: [], // 流程定义文件数据
      currentPage: 1, // 当前页码
      pageSize: 5, // 每页选定的显示条数
      total: 0
      // ********************************
    }
  },
  components: {
    AsideBar: resolve => require(['./aside-bar'], resolve)
  },
  created() {
    this.getBreadcrumb()
    this.getUser()
    this.getMenuList()
  },
  watch: {
    $route() {
      this.getBreadcrumb()
    },
    tableData(newValue, oldValue) {
      this.currentData = newValue.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
    },
    CurrentPage(newValue, oldValue) {
      this.currentPage = this.tableData.slice((newValue - 1) * this.pageSize, newValue * this.pageSize)
    }
  },
  methods: {
    // ****************翻页*************
    // 改变当前页码
    handleCurrentChange(val) {
      this.currentPage = val
      this.globalSearch()
    },
    // 序号
    getIndex(index) {
      return index + (this.currentPage - 1) * this.pageSize + 1
    },
    // *********************************
    // 退出登录
    loginout() {
      window.sessionStorage.clear()
      this.$router.replace('/')
    },
    about() {
      this.$router.replace('/card')
      this.showaside = false
    },
    // 快速新建下拉框跳转
    handleCommand(command) {
      if (command === 'loginout') {
        this.loginout()
      } else if (command === 'about') {
        this.about()
      }
    },
    // 获取面包屑导航
    getBreadcrumb() {
      // $route.matched一个数组 包含当前路由的所有嵌套路径片段的路由记录
      // var matched = this.$route.matched.filter(item => item.name)
      var matched = this.$route.matched
      this.levelList = matched
    },
    // 获取登录的个人信息
    getUser() {
      this.user = JSON.parse(window.sessionStorage.getItem('user'))
    },
    // 获取所有的菜单
    getMenuList() {
      if (!window.sessionStorage.getItem('menuList')) {
        this.getRequest('/sys/menu').then((resp) => {
          if (resp) {
            this.menulist = resp.data
            var list = ASE.encrypt(JSON.stringify(resp.data))
            console.log('靓仔的断点')
            console.log(list)
            // JSON.stringify(resp.data)
            window.sessionStorage.setItem('menuList', list)
            console.log('获取后端')
            console.log(window.sessionStorage.getItem('menuList'))
          }
        }, error => {
          console.log(error)
        })
      } else {
        this.menulist = JSON.parse(window.sessionStorage.getItem('menuList'))
        console.log('sessionStorage')
      }
    },
    // 搜索
    globalSearch() {
      this.getRequest('/common/es/' + this.inputString + '/' + this.currentPage + '/' + this.pageSize).then((resp) => {
        if (resp) {
          if (resp.msg === '搜索成功') {
            // this.sflag = 1
            var len = resp.data.length
            this.total = resp.data[len - 1].total
            this.tableDataArray3 = resp.data
            this.tableDataArray3.pop(resp.data[len - 1])
            this.gridData = this.tableDataArray3
            console.log(this.gridData)
            this.dialogTableVisible = true
          }
          console.log(resp)
        }
      }, error => {
        console.log(error)
      })
    },
    handleOpen(key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath)
    }
  }
}
</script>

<style>
/**===================== 顶部栏 start ===================*/
/** left   start */
.el-header .top{
  width: 90%;
  display: flex;
  justify-content: flex-start;
  padding: 1vh 0 !important;
}
/** 图标 */
.el-header .top img {
  display: block;
  width: 3vw;
  height: 5vh;
  margin: 2vh 0.5vw 2vh 0px;
}
/** 文字 */
.el-header div .top span {
  display: block;
  height: 100%;
  padding-top: 3vh;
  text-align: center;
  font: 1.3vw '宋体';
  font-weight: bolder;
  color: #FFFFFF;
}
/**   left   end */
.el-dropdown-link {
  position: relative;
  top: -10px;
  cursor: pointer;
  font: 1vw '宋体';
  color: #FFFFFF;
}
.el-icon-s-custom {
  font-size: 1vw;
}
.el-dropdown {
  margin-right: 10px;
}
.el-dropdown-menu__item {
  cursor: pointer;
  font: 1vw '宋体';
  margin-bottom: 10px;
}
/** 消息样式 */
.el-header div .message{
  margin-right: 1vw;
}
.search{
  /* display:inline-block;
  width: 3vw */
  margin-left: 43vw;
}
.el-select .el-input {
  width: 100%;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}
.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.box-card {
  width: 480px;
}
/**===================== 顶部栏 end ===================*/
</style>
